﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.LegalAffairs
{
    public partial class LegalAffairsSmallClass : HFController.BagePages
    {
        /// <summary>
        /// 列表
        /// </summary>
        public List<hf.Model.LegalAffairsSmallClass> list = new List<hf.Model.LegalAffairsSmallClass>();
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                string PageStr = "";
                list = LegalAffairsController.getController().getLegalAffairsSmallClassList(out PageStr);
                pager.Text = PageStr;

                //绑定法律事务大分类
                this.bindLegalAffairsBigClass();
                DropBigClass.Items.Insert(0, new ListItem("--请选择--", "0"));

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        LegalAffairsController.getController().doDelegeLegalAffairsSmallClass(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定法律事务大分类
        /// </summary>
        private void bindLegalAffairsBigClass()
        {
            DropBigClass.DataSource = LegalAffairsController.getController().getLegalAffairsBigClass(100);
            DropBigClass.DataValueField = "ID";
            DropBigClass.DataTextField = "ClassName"; 
            DropBigClass.DataBind(); //执行绑定
        }

        /// <summary>
        /// 得到法律事务大分类名称
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public string getLegalAffairsBigClassName(int ID)
        {
            //得到事务大分类
            hf.Model.LegalAffairsBiglClass model = LegalAffairsController.getController().getLegalAffairsBiglClassModel(ID);
            //返回名称
            //return model.ClassName;
            if (model != null)
            {
                return model.ClassName;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 绑定修改
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.LegalAffairsSmallClass model = LegalAffairsController.getController().getLegalAffairsSmallClassModel(ID);
            //输入框赋值
            tbSmallClass.Text = model.SmallClassName;
            DropBigClass.SelectedValue = model.BigClassName.ToString();
            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取Action参数，判断是修改还是保存
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改记录，刚才bindmodel()方法里，赋值的那两个
                LegalAffairsController.getController().doModifyLegalAffairsSmallClass(int.Parse(IDS.Value), tbSmallClass.Text,DropBigClass.SelectedValue);
            }
            else
            {
                LegalAffairsController.getController().doAddLegalAffairsSmallClass(tbSmallClass.Text,DropBigClass.SelectedValue);
            }
        }
    }
}